define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var time = Fast.api.query('time');
            var pay_type = Fast.api.query('pay_type');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wash/order/index' + location.search,
                    add_url: 'wash/order/add',
                    edit_url: 'wash/order/edit',
                    del_url: 'wash/order/del',
                    multi_url: 'wash/order/multi',
                    import_url: 'wash/order/import',
                    table: 'wash_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'sn', title: __('Sn'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'user.mobile', title: __('手机号'), operate: 'LIKE'},
                        {field: 'regional.nickname', title: __('Regional_id')},
                        {field: 'company.nickname', title: __('Company_id')},
                        {field: 'factory.nickname', title: __('Factory_id'), operate: 'LIKE'},
                        {field: 'shop.nickname', title: __('Shop_id'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'goods_name', title: __('Goods_name'), operate: 'LIKE'},
                        {field: 'goods_fee', title: __('Goods_fee'), operate:false},
                        {field: 'total_fee', title: __('Total_fee'), operate:false},
                        {field: 'pay_fee', title: __('Pay_fee'), operate:false},
                        {field: 'region_money', title: '分公司佣金', operate:false},
                        {field: 'subsidiary_money', title: '区域公司佣金', operate:false},
                        {field: 'shop_money', title: '驿站佣金', operate:false},
                        {field: 'factory_money', title: '门店佣金', operate:false},
                        {field: 'credit_fee', title: __('Credit_fee'), operate:false},
                        {field: 'discount', title: __('Discount'), operate:false},
                        {field: 'coupon_fee', title: __('Coupon_fee'), operate:false},
                        {field: 'urgent_fee', title: __('Urgent_fee'), operate:false},
                        {field: 'money_fee', title: __('Money_fee'), operate:false},
                        {field: 'send_fee', title: __('Send_fee'), operate:false},
                        {field: 'apply_time', title: __('Apply_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"-1":__('Status -1')}, custom: {"0": "purple", "1": "success", "2": "gray", "3": "warning", "4": "yellow", "-1": "danger"}, formatter: Table.api.formatter.normal},
                        {field: 'shop_status', title: __('Shop_status'), searchList: {"0":__('Shop_status 0'),"1":__('Shop_status 1'),"2":__('Shop_status 2'),"3":__('Shop_status 3'),"4":__('Shop_status 4')}, custom: {"0": "aqua", "1": "primary", "2": "navy", "3": "teal", "4": "olive"}, formatter: Table.api.formatter.normal},
                        {field: 'factory_status', title: __('Factory_status'), searchList: {"0":__('Factory_status 0'),"1":__('Factory_status 1'),"2":__('Factory_status 2')}, custom: {"0": "lime", "1": "fuchsia", "2": "blue"}, formatter: Table.api.formatter.normal},
                        {field: 'send_type', title: __('Send_type'), searchList: {"1":__('Send_type 1'),"2":__('Send_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'pay_type', title: __('Pay_type'), searchList: {"wxpay":__('Pay_type wxpay'),"money":__('Pay_type money')}, defaultValue: pay_type ? pay_type : '', formatter: Table.api.formatter.normal},
                        {field: 'canceltime', title: __('Canceltime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', defaultValue: time ? time : '', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'taketime', title: __('Taketime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'consignee', title: __('Consignee'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'is_urgent', title: __('Is_urgent'), searchList: {"0":__('Is_urgent 0'),"1":__('Is_urgent 1')}, formatter: Table.api.formatter.normal},
                        {field: 'shop_f_image', title: __('Shop_f_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'shop_b_image', title: __('Shop_b_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'shop_images', title: __('Shop_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'factory_f_image', title: __('Factory_f_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'factory_b_image', title: __('Factory_b_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'factory_images', title: __('Factory_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},

                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'wash/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'wash/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'wash/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
